<?php

	##################################################
	#
	# Copyright (c) 2004-2006 OIC Group, Inc.
	# Written and Designed by James Hunt
	#
	# This file is part of Exponent
	#
	# Exponent is free software; you can redistribute
	# it and/or modify it under the terms of the GNU
	# General Public License as published by the Free
	# Software Foundation; either version 2 of the
	# License, or (at your option) any later version.
	#
	# GPL: http://www.gnu.org/licenses/gpl.txt
	#
	##################################################

	class calendarmodule_config {
		function form($object) {
			$i18n = exponent_lang_loadFile('datatypes/calendarmodule_config.php');

			if (!defined('SYS_FORMS')) require_once(BASE.'subsystems/forms.php');
			exponent_forms_initialize();

			global $db;

			//eDebug($all_calendars);

			$form = new form();
			if (!isset($object->id)) {
				$object->enable_categories = 0;
				$object->enable_feedback = 0;
				$object->aggregate = array();
				$object->color_waiting = "#C0FFC0";
				$object->color_not_approve= "#FF8080";
				$object->need_permission= 0;
				$object->show_cat_whenajax= 0;
			} else {
				$form->meta('id',$object->id);
			}

			// setup the listbuilder arrays for calendar aggregation.	
			$loc = unserialize($object->location_data);
			$calendars = exponent_modules_getModuleInstancesByType('calendarmodule');
			$saved_aggregates = empty($object->aggregate) ? array() : unserialize($object->aggregate);
			$all_calendars = array();
			$selected_calendars = array();
			foreach ($calendars as $src => $cal) {
				$calendar_name = (empty($cal[0]->title) ? 'Untitled' : $cal[0]->title).' on page '.$cal[0]->section;
				if ($src != $loc->src) {
					if (in_array($src, $saved_aggregates)) {
						$selected_calendars[$src] = $calendar_name;
					} else {
						$all_calendars[$src] =  $calendar_name;
					}
				}
			}

			// setup the config form	
			$form->register(null,'',new htmlcontrol('<h1>General Configuration</h1><hr size="1" />'));
			$form->register('enable_categories',$i18n['enable_categories'],new checkboxcontrol($object->enable_categories,true));
			$form->register('enable_feedback',$i18n['enable_feedback'],new checkboxcontrol($object->enable_feedback,true));	
			
			if (substr($object->color_waiting,0,1) == "#")
				$object->color_waiting=substr($object->color_waiting,1);
			if (substr($object->color_not_approve,0,1) == "#")
				$object->color_not_approve=substr($object->color_not_approve,1); 
				   
			$form->register("color_waiting", "Color of Waiting events", new colorpicker($object->color_waiting,true));
			$form->register("color_not_approve", "Color of Not Approve events", new colorpicker($object->color_not_approve,false));
			$form->register("need_permission", "Need permission to view", new checkboxcontrol($object->need_permission,true));
			$form->register("show_cat_whenajax", "Show Category Name when in Ajax mode", new checkboxcontrol($object->show_cat_whenajax,true));
			
			$form->register(null,'',new htmlcontrol('<h1>Merge Calendars</h1><hr size="1" />'));
			$form->register('aggregate','Pull Events from These Other Calendars',new listbuildercontrol($selected_calendars,$all_calendars));
			$form->register('submit','',new buttongroupcontrol($i18n['save'],'',$i18n['cancel']));
			return $form;
		}

		function update($values,$object) {
			$object->enable_categories = (isset($values['enable_categories']) ? 1 : 0);
			$object->enable_feedback = (isset($values['enable_feedback']) ? 1 : 0);
			$object->aggregate = serialize(listbuildercontrol::parseData($values,'aggregate'));
			
			$object->color_waiting="#".$values['color_waiting'];
			$object->color_not_approve="#".$values['color_not_approve'];
			$object->need_permission=isset($values['need_permission'])?1:0;
			$object->show_cat_whenajax=isset($values['show_cat_whenajax'])?1:0;
			return $object;
		}
	}

?>
